//
//  RCSoftUpdate.m
//  CPICApp
//
//  Created by sinosoft on 13-4-1.
//  Copyright (c) 2013年 lizhiwei. All rights reserved.
//

#import "RCSoftUpdate.h"
#import "Config.h"
#import "RCAlertViewUtil.h"
#import "NSString+SBJson.h"
#import "NSObject+SBJson.h"
#import "AppDelegate.h"
#import "RCSystemService.h"

@implementation RCSoftUpdate
@synthesize progress;
@synthesize UpdateURL;

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
    }
    return self;
}
-(id)initSoftView
{
    self = [super init];
    return self;
}



-(void)setSoftUpdateRequest//向太保移动平台发送请求报文
{
//    NSString *strRequestCode=@"RequestCode=60801";
    NSMutableDictionary *dicRequest = [NSMutableDictionary dictionary];
    [dicRequest setObject:@"admin22" forKey:@"userAccount"];
    [dicRequest setObject:[RCSystemService currentVersion] forKey:@"version"];
    NSString *strRequestJson=[dicRequest JSONRepresentation];
//    strRequestJson=[NSString stringWithFormat:@"&RequestData=%@",strRequestJson];
//    NSString *strUrl=[NSString stringWithFormat:@"%@%@%@",Server_Url,strRequestCode,strRequestJson];
//    NSString *strUrlEncod = [strUrl stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];//字符串编码转换
//    NSURL *myurl = [NSURL URLWithString:strUrlEncod];//This method expects URLString to contain any necessary percent escape codes, which are ‘:’, ‘/’, ‘%’, ‘#’, ‘;’, and ‘@’. Note that ‘%’ escapes are translated via UTF-8.
//    NSLog(@"请求报文为:%@",strUrl);
    requestSoft= [[ASIFormDataRequest alloc] initWithURL:[NSURL URLWithString:Server_Url_Form]];
    //设置需要POST的数据，这里提交两个数据，A=a&B=b
    [requestSoft setPostValue:@"60801" forKey:@"RequestCode"];
    [requestSoft setPostValue:strRequestJson forKey:@"RequestData"];
    
    [requestSoft setDelegate:self];
    [requestSoft setDidFailSelector:@selector(ResponseFail:)];
    [requestSoft setDidFinishSelector:@selector(ResonseSuccess:)];
    [requestSoft startAsynchronous];
    
    showProgress = YES;
    if (showProgress) {
        NSLog(@"88888");
        //[NSThread sleepForTimeInterval:1];
        progress= [[MBProgressHUD alloc]initWithView:self];
        [self addSubview:progress];
        [self bringSubviewToFront:progress];
        progress.labelText = @"查询中...";
        [progress show:YES];
        showProgress = NO;
    };
    
    
}

- (void)ResonseSuccess:(ASIHTTPRequest *)request
{
    if (!showProgress)
    {
        [progress removeFromSuperview];
        progress = nil;
    }
    NSString *responseString = [request responseString];
    NSLog(@"responseString %@",responseString);
    NSMutableDictionary *dictResponse=[responseString JSONValue];
    NSLog(@"dictResponse  %@",dictResponse);
    serverceVersion=[[NSString alloc]initWithFormat:@"%@",[dictResponse objectForKey:@"version"]];
    strUpdateURL=[[NSString alloc]initWithFormat:@"%@",[dictResponse objectForKey:@"appUrl"]];
    strUpdateURL=[strUpdateURL stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    NSLog(@"服务器版本是%@",serverceVersion);
    NSLog(@"更新地址是%@",strUpdateURL);
    BOOL isOldVersion = NO;
    isOldVersion = [RCSystemService isOldVersionCompareWithVersionFromServer:serverceVersion];
    NSString *strUpdateContent=[[NSString alloc]initWithFormat:@"有新版本,是否需要更新,更新内容:%@",[dictResponse objectForKey:@"versionContent"]];
    if (isOldVersion) {
        UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"消息"
                                                       message:strUpdateContent
                                                      delegate:self
                                             cancelButtonTitle:@"以后再说"
                                             otherButtonTitles:@"现在更新",nil];
        [alert setTag:1];
        [alert show];
    }
    else
    {
        //[self justShowMessage:@"当前已是最新版本!" title:@"提示"];
    }
}
- (void)ResponseFail:(ASIHTTPRequest *)request
{
    if (!showProgress)
    {
        [progress removeFromSuperview];
        progress = nil;
    }
    NSLog(@"服务器连接失败!");
    //[self justShowMessage:@"服务器连接失败" title:@"注意"];
}
#pragma mark--
#pragma mark-- UIAlertViewDelegate
-(void) alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if (alertView.tag==1) {
        if (buttonIndex==0) {
            
        }
        else
        {
            if (strUpdateURL.length==0) {
                [self justShowMessage:@"更新地址为空" title:@"注意"];
            }
            else
            {
                UpdateURL=[NSURL URLWithString:strUpdateURL];
                [[UIApplication sharedApplication]openURL:UpdateURL];
            }
        }
    }
}

-(void) dealloc
{
    requestSoft.delegate=nil;
    [progress release];
    [UpdateURL release];
    [super dealloc];
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
    // Drawing code
}
*/

@end
